using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolBase : MonoBehaviour
{
    //自动释放时间
    protected float releaseTime;

    //上次释放资源的时间/毫微秒
    protected long lastReleaseTime = 0;

    //对象池列表
    protected List<PoolObject> poolObjects;

    private void Start()
    {
        lastReleaseTime = System.DateTime.Now.Ticks;
    }

    private void Update()
    {
        if(System.DateTime.Now.Ticks - lastReleaseTime >= releaseTime * 10000000)
        {
            lastReleaseTime = System.DateTime.Now.Ticks;
            Release();
        }
    }

    //初始化对象池
    public void Init(float _time)
    {
        releaseTime = _time;
        poolObjects = new List<PoolObject>();
    }

    //取出对象
    public virtual Object Spwan(string name)
    {
        foreach (var obj in poolObjects)
        {
            if(obj.Name == name)
            {
                poolObjects.Remove(obj);
                return obj.Object;
            }
        }
            
        return null;
    }

    //回收对象
    public virtual void UnSpwan(string name, Object obj) 
    {
        PoolObject poolObject = new PoolObject(name, obj);
        poolObjects.Add(poolObject);
    }

    public virtual void Release()
    {

    }
}
